<template>
    <Modal
            width="800"
            v-model="visible"
            :title="title"
            :key="key"
    >
        <div style="width: 80%;margin: 0 auto;" :loading="Loading">
            <Form :label-width="130" :disabled="disableSubmit" ref="formValidate" :model="formValidate"
                  :rules="ruleValidate">
                <Form-item label="任务名称:" prop="name">
                    <Input v-model="formValidate.name" placeholder="请输入任务名称" :disabled="disableEdit"/>
                </Form-item>
                <Form-item label="任务类名:" prop="className">
                    <Input v-model="formValidate.className" placeholder="请输入任务类名" :disabled="disableEdit"/>
                </Form-item>
                <Form-item label="crom表达式:" prop="cron">
                    <Input v-model="formValidate.cron" placeholder="请输入crom表达式"/>
                </Form-item>
                <Form-item label="参数信息">
                    <Input v-model="formValidate.arg" placeholder="请输入参数信息"/>
                </Form-item>
                <Form-item label="描述">
                    <Input v-model="formValidate.remarks" placeholder="请输入描述"/>
                </Form-item>
            </Form>
        </div>
        <div slot="footer">
            <Button @click="cancel">取消</Button>
            <template v-if="!disableSubmit">
                <Button type="primary" @click="ok">确定</Button>
            </template>
        </div>
    </Modal>
</template>

<script>
    import {getAction, postAction} from '@/api/request'

    export default {
        data() {
            return {
                modal1: false,
                title: "",
                visible: false,
                disableSubmit: false,
                Loading: false,
                formValidate: {},
                sceneList: {},
                actionList: {},
                disableEdit: true,
                key: "",
                ruleValidate: {
                    name: [
                        {required: true, message: '任务名称不能为空', trigger: 'blur'}
                    ],
                    className: [
                        {required: true, message: '任务类名不能为空', trigger: 'blur'}
                    ],
                    cron: [
                        {required: true, message: 'crom表达式不能为空', trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            ok() {
                var that = this;
                this.$refs['formValidate'].validate((valid) => {
                    if (valid) {
                        that.submitFrom();
                    }
                })
            },

            submitFrom() {
                var that = this;
                that.Loading = true;
                var url = "/api/ac/subsistence/QuartzManagerServiceImpl/insert";
                if (this.formValidate.id) {
                    url = "/api/ac/subsistence/QuartzManagerServiceImpl/update"
                }
                postAction(url, this.formValidate).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg);
                    } else {
                        this.$emit('ok');
                        this.visible = false;
                    }

                }).finally(() => {
                    that.Loading = false;
                });
            },
            cancel() {
                this.visible = false;
            },
            add() {

                this.edit({});
            },
            edit(row) {
                this.visible = true;
                if (row.id) {
                    this.disableEdit = true;
                } else {
                    this.disableEdit = false;
                }
                this.formValidate = Object.assign({}, row);
            },
        }
    }
</script>

<style scoped>

</style>